<?php


namespace app\command;


use app\common\Sms;
use app\services\utils\SmsService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class CupboardAbnormalSms extends Command {

    protected $type = [
        3 => '设备断电',
        8 => '设备断电',
        10 => '柜门异常',
        11 => '柜门异常',
        12 => '传感器脱落',
    ];

    protected function configure() {
        // 指令配置
        $this->setName('send_cupboard_abnormal_sms')->setDescription('货柜异常发送短信给运营');
    }

    protected function execute(Input $input, Output $output) {

        return $output->writeln('暂停执行');
        // 指令输出
        $maxId = Db::name('cupboard_abnormal_sms_record')->max('msg_id');
        if (!$maxId) {
            $time = '2022-10-12 18:30:00';
        }

        $where = [];
//        $where[] = ['type', 'in', [3, 10, 11, 12]];
        $where[] = ['type', 'in', [8]];
        if ($maxId) {
            $where[] = ['id', '>', $maxId];
        }
        if (isset($time) && $time) {
            $where[] = ['created_at', '>=', $time];
        }
        $list = Db::connect('cupboard')->table('msg')->where($where)->field("id,type,cupboard_id")->select()->toArray();
        if (!$list) {
            $output->writeln('没有可执行的数据');
            return;
        }

        foreach ($list as $key => $value) {
            $cupboardInfo = Db::connect('cupboard')->table('cupboard')->where('id', $value['cupboard_id'])->find();
            if (!$cupboardInfo) {
                continue;
            }
            $district = $cupboardInfo['district'];
            if (!$district) {
                continue;
            }
            $areaId = Db::name('region')->where('name', $district)->value('id');
            if (!$areaId) {
                continue;
            }
            $userIds = Db::name('operator_area')->where('area_id', $areaId)->group("user_id")->column("user_id");
            if (!$userIds) {
                continue;
            }
            $franchiserInfo = Db::connect('cupboard')->table('franchiser')->where('id', $cupboardInfo['franchiser_id'])->find();
            if (!$franchiserInfo) {
                continue;
            }

            foreach ($userIds as $id) {
                $userPhone = Db::name('admin_user')->where('id', $id)->value('phone');
                if (!$userPhone) {
                    continue;
                }
                $arr = [
//                    "franchiser_name" => $cupboardInfo['city'] . $cupboardInfo['district'] . '，' . $cupboardInfo['name'] . '，商户手机号' . $franchiserInfo['phone'],//"郑州市金水区，198豫见她土豆粉A1，商户手机号13800138000",
                    "franchiser_name" => $cupboardInfo['name'],//"郑州市金水区，198豫见她土豆粉A1，商户手机号13800138000",
                    "phone" => $franchiserInfo['phone'],
                    "imei" => $cupboardInfo['imei'],
                    "problem" => $this->type[$value['type']]
                ];
                $json = json_encode($arr);
                $msg = '';
                try {
                    SmsService::sendSms(Sms::ABNORMAL_SENSOR, $userPhone, $json, config('sms.sign_name'));
                } catch (\Exception $exception) {
                    $msg = $exception->getMessage();
                }
                $data = [
                    'msg_id' => $value['id'],
                    'send_phone' => $userPhone,
                    'content' => $json,
                    'msg' => $msg,
                    'create_time' => date('Y-m-d H:i:s')
                ];
                Db::name('cupboard_abnormal_sms_record')->insert($data);
            }

        }
        $output->writeln('货柜异常发送短信完成');
    }
}